package cn.zyq.ykb.service.product.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.product.ProductLinkMapper;
import cn.zyq.ykb.beans.product.ProductLink;
import cn.zyq.ykb.service.product.ProductLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品链接Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-11-02
 */
@Service
public class ProductLinkServiceImpl  extends BaseService implements ProductLinkService {

    @Autowired
    private ProductLinkMapper productLinkMapper;

    /**
     * 查询商品链接
     *
     * @param productLinkID 商品链接主键
     * @return 商品链接
     */
    @Override
    public ProductLink selectProductLinkByProductLinkID(Integer productLinkID) {
        ProductLink productLink = productLinkMapper.selectByPrimaryKey(productLinkID);
        if (productLink.getDeleted() == 1) {
            return null;
        }
        return productLink;
    }

    /**
     * 查询商品链接
     *
     * @param serviceID 业务ID
     * @return 商品链接
     */
    @Override
    public ProductLink selectProductLinkByServiceID(String serviceID) {
        ProductLink param = new ProductLink();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.productLinkMapper.selectOne(param);
    }

    /**
     * 查询商品链接列表
     *
     * @param pageParam
     * @return 商品链接
     */
    @Override
    public PageInfo<ProductLink> selectProductLinkList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ProductLink> list = this.productLinkMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询商品链接列表
     *
     * @param pageParam
     * @return 商品链接
     */
    @Override
    public List<ProductLink> selectProductLinkAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.productLinkMapper.list(pageParam.getKv());
    }
    /**
     * 新增商品链接
     *
     * @param param 商品链接
     * @return 结果
     */
    @Override
    public int insertProductLink(ProductLink param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.productLinkMapper.insertSelective(param);
    }

    /**
     * 修改商品链接
     *
     * @param param 商品链接
     * @return 结果
     */
    @Override
    public int updateProductLink(ProductLink param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.productLinkMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除商品链接
     *
     * @param productLinkIDs 需要删除的商品链接主键
     * @return 结果
     */
    @Override
    public int deleteProductLinkByProductLinkIDs(Integer[] productLinkIDs) {
        int i = 0;
        for (Integer productLinkID : productLinkIDs) {
            i += this.deleteProductLinkByProductLinkID(productLinkID);
        }
        return i;
    }

    /**
     * 删除商品链接信息
     *
     * @param productLinkID 商品链接主键
     * @return 结果
     */
    @Override
    public int deleteProductLinkByProductLinkID(Integer productLinkID) {
        ProductLink productLink = this.selectProductLinkByProductLinkID(productLinkID);
        if (productLink == null) {
            return 0;
        } else {
            ProductLink storeInfo = new ProductLink();
            storeInfo.setProductLinkID(productLinkID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.productLinkMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
